package club.banyuan.hrms.controller;

import club.banyuan.hrms.dao.AdminDao;
import club.banyuan.hrms.domain.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdminController {

    private AdminDao adminDao;


    public AdminController(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    @Autowired
//    @Qualifier("adminDbDaoImpl")
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    @RequestMapping("/admin")
    public String admin(Integer id, Model model) {
        if (id != null) {
            Admin admin = adminDao.getAdminById(id);
            if (admin != null) {
                model.addAttribute("username", admin.getUsername());
                model.addAttribute("rolename", admin.getRolename());
                model.addAttribute("password", admin.getPassword());
            }
        }
        return "admin";
    }

    @RequestMapping(path = "/admins", method = RequestMethod.POST)
    public ModelAndView addAdmin(String username, String rolename, String password) {
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setRolename(rolename);
        admin.setPassword(password);
        adminDao.addAdmin(admin);

        ModelAndView mav = new ModelAndView("admin");
        mav.addObject("id", admin.getId());
        mav.addObject("username", admin.getUsername());
        mav.addObject("rolename", admin.getRolename());
        mav.addObject("password", admin.getPassword());
        return mav;
    }
}
